using System;
using System.Collections.Generic;
using System.Text;
using SqlTools.DataAccessLibrary;
using System.Data.SqlClient;
using System.Data;

namespace SqlTools.SampleDAL.Data
{
	public class DbUser_Role : DbBase<User_RolePrimaryKey, User_Role>
	{
		public DbUser_Role()
			: base("daltest")
		{
		}

		public DbUser_Role(string connectionStringName)
			: base(connectionStringName)
		{

		}

		#region Select
		public override User_Role SelectByPK(User_RolePrimaryKey key)
		{
			User_Role urole = null;
			using ( SqlConnection conn = new SqlConnection(_connectionString) )
			{
				SqlCommand cmd = new SqlCommand("daltest_User_Role_SelectByPK", conn);
				cmd.CommandType = CommandType.StoredProcedure;

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = key.UserID;
				cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = key.RoleID;

				conn.Open();

				SqlDataReader sdr = cmd.ExecuteReader();

				if ( sdr.Read() )
				{
					urole = User_Role.CreateFromReader(sdr);
					urole._ConnectionStringName = _connectionStringName;
				}
				sdr.Close();

				conn.Close();
			}

			return urole;
		}

		public DbList<User_Role> SelectByUserID(int userID)
		{
			DbList<User_Role> uroles = new DbList<User_Role>();

			using ( SqlConnection conn = new SqlConnection(_connectionString) )
			{
				SqlCommand cmd = new SqlCommand("daltest_User_Role_SelectByUserID", conn);
				cmd.CommandType = CommandType.StoredProcedure;

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = userID;

				conn.Open();

				SqlDataReader sdr = cmd.ExecuteReader();

				while ( sdr.Read() )
				{
					User_Role urole = User_Role.CreateFromReader(sdr);
					urole._ConnectionStringName = _connectionStringName;

					uroles.Add(urole);
				}
				sdr.Close();

				conn.Close();
			}

			return uroles;
		}

		public DbList<User_Role> SelectByRoleID(int roleID)
		{
			DbList<User_Role> uroles = new DbList<User_Role>();

			using ( SqlConnection conn = new SqlConnection(_connectionString) )
			{
				SqlCommand cmd = new SqlCommand("daltest_User_Role_SelectByRoleID", conn);
				cmd.CommandType = CommandType.StoredProcedure;

				cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = roleID;

				conn.Open();

				SqlDataReader sdr = cmd.ExecuteReader();

				while ( sdr.Read() )
				{
					User_Role urole = User_Role.CreateFromReader(sdr);
					urole._ConnectionStringName = _connectionStringName;

					uroles.Add(urole);
				}
				sdr.Close();

				conn.Close();
			}

			return uroles;
		}
		#endregion

		#region Insert
		public override User_RolePrimaryKey Insert(User_Role urole, SqlConnection conn, SqlTransaction trans)
		{
			urole._ConnectionStringName = _connectionStringName;

			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_User_Role_Insert", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = urole.Key.UserID;
				cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = urole.Key.RoleID;

				cmd.ExecuteNonQuery();

				urole._UpdateState(ObjectState.None);
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}

			return urole.Key;
		}
		#endregion

		#region Update
		public override int Update(User_Role urole, SqlConnection conn, SqlTransaction trans)
		{
			urole._ConnectionStringName = _connectionStringName;

			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_User_Role_Update", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = urole.Key.UserID;
				cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = urole.Key.RoleID;

				int rows = cmd.ExecuteNonQuery();

				urole._UpdateState(ObjectState.None);

				return rows;
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}
		}
		#endregion

		#region Delete
		public override int Delete(User_Role urole, SqlConnection conn, SqlTransaction trans)
		{
			bool destroyConn = false;

			if ( conn == null )
			{
				destroyConn = true;
				conn = new SqlConnection(_connectionString);
			}

			try
			{
				SqlCommand cmd = new SqlCommand("daltest_User_Role_Delete", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Transaction = trans;

				if ( conn.State != ConnectionState.Open )
				{
					conn.Open();
				}

				cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = urole.Key.UserID;
				cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = urole.Key.RoleID;

				int rows = cmd.ExecuteNonQuery();

				urole._UpdateState(ObjectState.Deleted);

				return rows;
			}
			finally
			{
				if ( destroyConn && conn.State == ConnectionState.Open )
				{
					conn.Close();
				}

				if ( destroyConn )
				{
					conn.Dispose();
				}
			}
		}
		#endregion
	}
}
